﻿Public Class Xe
    Public Shared Function DanhSachMau() As DataTable
        Return db.QueryTable("Select * from MauXe")
    End Function
    ''' <summary>
    ''' cai nay viet chung, neu co nhieu bang thi can inner join du het, trong truong hop nay tam thoi t viet don gian, chua inner join j ca 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DanhSachBienSo() As DataTable
        Return db.QueryTable("select BienSo from Xe")
    End Function
    Public Shared Function Them(ByVal BienSo As String, ByVal TenXe As String, ByVal Gia As Double, _
                                ByVal NamSX As Integer _
                                , ByVal MaNCC As Integer, ByVal MaLoai As Integer, _
                                ByVal MaHang As Integer, ByVal MaMau As Integer _
                                , ByVal MaTT As Integer) As Boolean
        BienSo = EscapeString(BienSo)
        TenXe = EscapeString(TenXe)
        Return db.Query("insert into Xe(BienSo,TenXe,Gia,NamSX,MaNCC,MaLoai,MaHang,MaMau,MaTT) " & _
                             "Values ('" & BienSo & _
                                    "','" & TenXe & _
                                    "','" & Gia & _
                                    "','" & NamSX & _
                                    "','" & MaNCC & _
                                    "','" & MaLoai & _
                                    "','" & MaHang & _
                                    "','" & MaMau & _
                                    "','" & MaTT & _
                                    "')")
    End Function
    Public Shared Function DanhSach() As DataTable
        Return db.QueryTable("SELECT     Xe.BienSo, Xe.TenXe, Xe.Gia, Xe.NamSX, Xe.MaNCC, Xe.MaLoai,Xe.MaMau, Xe.MaHang, Xe.MaTT, Xe.Ranh, NhaCungCap.TenNCC, MauXe.TenMau, LoaiXe.TenLoai, " & _
                                        "TinhTrangXe.MoTa, HangSanXuat.TenHang " & _
                            "FROM         Xe INNER JOIN " & _
                                        "NhaCungCap ON Xe.MaNCC = NhaCungCap.MaNCC INNER JOIN " & _
                                        "MauXe ON Xe.MaMau = MauXe.MaMau INNER JOIN " & _
                                        "LoaiXe ON Xe.MaLoai = LoaiXe.MaLoai INNER JOIN " & _
                                        "TinhTrangXe ON Xe.MaTT = TinhTrangXe.MaTT INNER JOIN " & _
                                        "HangSanXuat ON Xe.MaHang = HangSanXuat.MaHang")

    End Function
    Public Shared Function CapNhat(ByVal BienSo As String, ByVal TenXe As String, ByVal Gia As Double, _
                            ByVal NamSX As Integer _
                            , ByVal MaNCC As Integer, ByVal MaLoai As Integer, _
                            ByVal MaHang As Integer, ByVal MaMau As Integer _
                            , ByVal MaTT As Integer) As Boolean
        BienSo = EscapeString(BienSo)
        TenXe = EscapeString(TenXe)
        Return db.Query("update Xe " & _
                                    " set TenXe = '" & TenXe & _
                                    "' set Gia=" & Gia & _
                                    " set NamSX=" & NamSX & _
                                    " set MaNCC=" & MaNCC & _
                                    " set MaLoai=" & MaLoai & _
                                    " set MaHang=" & MaHang & _
                                    " set MatMau=" & MaMau & _
                                    " set MaTT=" & MaTT & _
                                    " where BienSo='" & BienSo & _
                                    "'")
    End Function
    Public Shared Function Xoa(ByVal BienSo As String)
        BienSo=escapestring(BienSo)
        Return db.Query("delete from Xe where BienSo='" & BienSo & "'")
    End Function
    Public Shared Function TimKiem(ByVal TuKhoa As String) As DataTable
        TuKhoa = EscapeString(TuKhoa)
        Return db.QueryTable("SELECT     Xe.BienSo, Xe.TenXe, Xe.Gia, Xe.NamSX, Xe.MaNCC, Xe.MaLoai,Xe.MaMau, Xe.MaHang, Xe.MaTT, Xe.Ranh, NhaCungCap.TenNCC, MauXe.TenMau, LoaiXe.TenLoai, " & _
                                        "TinhTrangXe.MoTa, HangSanXuat.TenHang " & _
                            "FROM         Xe INNER JOIN " & _
                                        "NhaCungCap ON Xe.MaNCC = NhaCungCap.MaNCC INNER JOIN " & _
                                        "MauXe ON Xe.MaMau = MauXe.MaMau INNER JOIN " & _
                                        "LoaiXe ON Xe.MaLoai = LoaiXe.MaLoai INNER JOIN " & _
                                        "TinhTrangXe ON Xe.MaTT = TinhTrangXe.MaTT INNER JOIN " & _
                                        "HangSanXuat ON Xe.MaHang = HangSanXuat.MaHang " & _
                                        "where xe.BienSo like '%" & TuKhoa & "%' or " & _
                                        "TenXe like '%" & TuKhoa & "%' or " & _
                                        "TenHang like '%" & TuKhoa & "%' or " & _
                                        "TenNCC like '%" & TuKhoa & "%'")

    End Function
End Class
